package cn.yhz.pethome.dept.service.impl;

import cn.yhz.pethome.base.service.impl.BaseServiceImpl;
import cn.yhz.pethome.base.util.ResultBean;
import cn.yhz.pethome.dept.Query.DepartmentQuery;
import cn.yhz.pethome.dept.domain.Department;
import cn.yhz.pethome.dept.mapper.IDepartmentMapper;
import cn.yhz.pethome.dept.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department>implements IDepartmentService {

    @Autowired
    private IDepartmentMapper departmentMapper;


    @Override
    public List<Department> getTree() {
        /**
         * 独有的
         */
        //第一步查询所有部门
        List<Department> deptall = departmentMapper.selectAll();
        //拿List专门装顶级部门
        List<Department> Dept = new ArrayList<>();
        for (Department department : deptall) {
            //判断parent_id == null 就是顶级部门因为 顶级部门没有父id
            if(department.getParent_id()==null){
                Dept.add(department);
            }else{
                //不是顶级部门
                for (Department department1 : deptall) {
                    //找自己的父
                    if(department.getParent_id()==department1.getId()){
                        department1.getChildren().add(department);
                    }
                }
            }
        }
        return Dept;
    }
}
